#include "main.h"

typedef struct {
    int id;             // 学号
    char name[NAME_LENGTH]; // 姓名
    float score;        // 分数
} Student;

Student students[MAX_STUDENTS];
int student_count = 0;

void add_student() {
    if (student_count >= MAX_STUDENTS) {
        printf("无法添加更多学生，已达到最大数量！\n");
        return;
    }

    Student new_student;
    printf("请输入学生学号: ");
    scanf("%d", &new_student.id);
    printf("请输入学生姓名: ");
    scanf("%s", new_student.name);
    printf("请输入学生成绩: ");
    scanf("%f", &new_student.score);

    students[student_count++] = new_student;
    printf("学生信息添加成功！\n");
}

void display_students() {
    if (student_count == 0) {
        printf("当前没有学生信息！\n");
        return;
    }

    printf("\n所有学生信息:\n");
    printf("学号\t姓名\t\t成绩\n");
    for (int i = 0; i < student_count; i++) {
        printf("%d\t%-10s\t%.2f\n", students[i].id, students[i].name, students[i].score);
    }
}

void find_student() {
    int id;
    printf("请输入要查找的学生学号: ");
    scanf("%d", &id);

    for (int i = 0; i < student_count; i++) {
        if (students[i].id == id) {
            printf("学生信息:\n");
            printf("学号: %d\n姓名: %s\n成绩: %.2f\n", students[i].id, students[i].name, students[i].score);
            return;
        }
    }
    printf("未找到学号为 %d 的学生！\n", id);
}

void delete_student() {
    int id;
    printf("请输入要删除的学生学号: ");
    scanf("%d", &id);

    for (int i = 0; i < student_count; i++) {
        if (students[i].id == id) {
            for (int j = i; j < student_count - 1; j++) {
                students[j] = students[j + 1];
            }
            student_count--;
            printf("学号为 %d 的学生信息已删除！\n", id);
            return;
        }
    }
    printf("未找到学号为 %d 的学生！\n", id);
}

void menu() {
    printf("\n===== 学生成绩管理系统 =====\n");
    printf("1. 添加学生\n");
    printf("2. 显示所有学生\n");
    printf("3. 查找学生\n");
    printf("4. 删除学生\n");
    printf("5. 退出系统\n");
    printf("==========================\n");
    printf("请输入选项: ");
}

int main() {
    int choice;

    while (1) {
        menu();
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                add_student();
                break;
            case 2:
                display_students();
                break;
            case 3:
                find_student();
                break;
            case 4:
                delete_student();
                break;
            case 5:
                printf("退出系统。\n");
                exit(0);
            default:
                printf("无效的选项，请重新输入！\n");
        }
    }
    return 0;
}

//

